package com.srv.servlet;

import junit.framework.Assert;
import junit.framework.TestCase;

import java.io.FileInputStream;
import java.io.File;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;



public class ServletNodeUpdateTestCase extends TestCase {
	
	private static String validNodeID="da64d24455f94d9d8d6d6cceb174bf45";
	
	public ServletNodeUpdateTestCase(String name){
		super(name);
	}
	
	//85f01aeae2a448879dc7b3bfdc26f247
	
	public void testInvalidArgument_textIsTooLong() { 	
		String expect = "{\"r\":\"fail\",\"m\":\"text is too long\"}";	
		try{
			Part[] thePart=new  Part[4];						
			thePart[0]=new StringPart("id",validNodeID);
			thePart[1]=new StringPart("text","a12345678901234123456789012341234567890123412345678901234123456789012341234567890123412345678901234123456789012341234567890123412345678901234","UTF-8");
			thePart[2]=new StringPart("lat","0.111");
			thePart[3]=new StringPart("long","-111.111");
			//thePart[4]=new  FilePart( "img" ,  "test1.jpg" ,  new  File( "c:/test1.jpg" ),null,"UTF-8");
			PostMethod postMethod  =   new  PostMethod(TestConfig.servlet_prefix+"/travel/node/update");
			MultipartRequestEntity mrp =   new  MultipartRequestEntity( thePart, postMethod.getParams());
			postMethod.setRequestEntity(mrp);
			 // 执行postMethod 
          HttpClient httpClient  =   new  HttpClient();
          httpClient.executeMethod(postMethod);
          String theResult=postMethod.getResponseBodyAsString();          
		  Assert.assertEquals(expect,postMethod.getResponseBodyAsString());	
		  
		  //测试中文
		  thePart=new  Part[4];						
		thePart[0]=new StringPart("id",validNodeID);
		thePart[1]=new StringPart("text","我2345678901234123456789012341234567890123412345678901234123456789012341234567890123412345678901234123456789012341234567890123412345678901234","UTF-8");
		thePart[2]=new StringPart("lat","0.111");
		thePart[3]=new StringPart("long","-111.111");
		//thePart[4]=new  FilePart( "img" ,  "test1.jpg" ,  new  File( "c:/test1.jpg" ),null,"UTF-8");
		postMethod  =   new  PostMethod(TestConfig.servlet_prefix+"/travel/node/update");
		mrp =   new  MultipartRequestEntity( thePart, postMethod.getParams());
		postMethod.setRequestEntity(mrp);
		// 执行postMethod 
//        
//        httpClient.executeMethod(postMethod);
//        theResult=postMethod.getResponseBodyAsString();     
//        System.out.println(theResult);
//		  Assert.assertEquals(expect,postMethod.getResponseBodyAsString());	
		  
		}catch(Exception e){
			e.printStackTrace();
		}		
    }
	public void testInvalidArgument_lat_long_fomat() { 	
		String expect = "{\"r\":\"fail\",\"m\":\"parameter is invalid\"}";	
		try{
			Part[] thePart=new  Part[4];						
			thePart[0]=new StringPart("id",validNodeID);
			thePart[1]=new StringPart("text","ssfsdf","UTF-8");
			thePart[2]=new StringPart("lat","0.111a");
			thePart[3]=new StringPart("long","-111.111");
			//thePart[4]=new  FilePart( "img" ,  "test1.jpg" ,  new  File( "c:/test1.jpg" ),null,"UTF-8");
			PostMethod postMethod  =   new  PostMethod(TestConfig.servlet_prefix+"/travel/node/update");
			MultipartRequestEntity mrp =   new  MultipartRequestEntity( thePart, postMethod.getParams());
			postMethod.setRequestEntity(mrp);
			 // 执行postMethod 
          HttpClient httpClient  =   new  HttpClient();
          httpClient.executeMethod(postMethod);
          String theResult=postMethod.getResponseBodyAsString();          
		  Assert.assertEquals(expect,postMethod.getResponseBodyAsString());	
		  
		  //测试中文
		  thePart=new  Part[4];						
		thePart[0]=new StringPart("id",validNodeID);
		thePart[1]=new StringPart("text","我sdf","UTF-8");
		thePart[2]=new StringPart("lat","0.111");
		thePart[3]=new StringPart("long","-111.111a");
		//thePart[4]=new  FilePart( "img" ,  "test1.jpg" ,  new  File( "c:/test1.jpg" ),null,"UTF-8");
		postMethod  =   new  PostMethod(TestConfig.servlet_prefix+"/travel/node/update");
		mrp =   new  MultipartRequestEntity( thePart, postMethod.getParams());
		postMethod.setRequestEntity(mrp);
		// 执行postMethod 
        
        httpClient.executeMethod(postMethod);
        theResult=postMethod.getResponseBodyAsString();     
        System.out.println(theResult);
		  Assert.assertEquals(expect,postMethod.getResponseBodyAsString());	
		  
		}catch(Exception e){
			e.printStackTrace();
		}		
    }
	public void testInvalidArgument_id() { 	
		String expect = "{\"r\":\"fail\",\"m\":\"id is invalid\"}";	
		try{
			//没有设置ID
			Part[] thePart=new  Part[3];						
			//thePart[0]=new StringPart("id","ds");
			thePart[0]=new StringPart("text","12345678901234123456789012341234567890123412345678901234123456789012341234567890123412345678901234123456789012341234567890123412345678901234","UTF-8");
			thePart[1]=new StringPart("lat","0.111");
			thePart[2]=new StringPart("long","-111.111");
			//thePart[4]=new  FilePart( "img" ,  "test1.jpg" ,  new  File( "c:/test1.jpg" ),null,"UTF-8");
			PostMethod postMethod  =   new  PostMethod(TestConfig.servlet_prefix+"/travel/node/update");
			MultipartRequestEntity mrp =   new  MultipartRequestEntity( thePart, postMethod.getParams());
			postMethod.setRequestEntity(mrp);
			 // 执行postMethod 
          HttpClient httpClient  =   new  HttpClient();
          httpClient.executeMethod(postMethod);          
		  Assert.assertEquals(expect,postMethod.getResponseBodyAsString());	
		  
		  //ID的值不对
		  thePart=new  Part[4];						
			thePart[0]=new StringPart("id","ds");
			thePart[1]=new StringPart("text","12345678901234123456789012341234567890123412345678901234123456789012341234567890123412345678901234123456789012341234567890123412345678901234","UTF-8");
			thePart[2]=new StringPart("lat","0.111");
			thePart[3]=new StringPart("long","-111.111");
			//thePart[4]=new  FilePart( "img" ,  "test1.jpg" ,  new  File( "c:/test1.jpg" ),null,"UTF-8");
			postMethod  =   new  PostMethod(TestConfig.servlet_prefix+"/travel/node/update");
			mrp =   new  MultipartRequestEntity( thePart, postMethod.getParams());
			postMethod.setRequestEntity(mrp);
			 // 执行postMethod 
        
        httpClient.executeMethod(postMethod);          
		  Assert.assertEquals(expect,postMethod.getResponseBodyAsString());	
		  
		}catch(Exception e){
			e.printStackTrace();
		}		
    }
	//85f01aeae2a448879dc7b3bfdc26f247
	public void testValidArgument() { 				
		try{
			Part[] thePart=new  Part[5];						
			thePart[0]=new StringPart("id",validNodeID);
			thePart[1]=new StringPart("text","这是一个很重要的text，是不受","UTF-8");
			thePart[2]=new StringPart("lat","0.111");
			thePart[3]=new StringPart("long","-111.111");
			thePart[4]=new  FilePart( "img" ,  "test1.jpg" ,  new  File( "c:/test1.jpg" ),null,"UTF-8");
			PostMethod postMethod  =   new  PostMethod(TestConfig.servlet_prefix+"/travel/node/update");
			MultipartRequestEntity mrp =   new  MultipartRequestEntity( thePart, postMethod.getParams());
			postMethod.setRequestEntity(mrp);
			 // 执行postMethod 
          HttpClient httpClient  =   new  HttpClient();
          httpClient.executeMethod(postMethod);
         // System.out.println(new String(postMethod.getResponseBodyAsString().getBytes(),"UTF-8"));
		Assert.assertEquals("{\"r\":\"ok\"}",postMethod.getResponseBodyAsString());		
		}catch(Exception e){
			e.printStackTrace();
		}		
    }
}
